/**
 * 
 */
package com.dashboard.service;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.dashboard.dao.OrganizacaoDaoImpl;
import com.dashboard.domain.Organizacao;
import com.dashboard.util.tests.BDUtils;
import com.dashboard.util.tests.PropertyTestsUtils;

/**
 * @author jneves
 *
 */
public class OrganizationServiceReadTest 
{
	
	private static final String LOCAL_ARQ = "testFiles/organizationService/";
	
	private static BDUtils bdUtils;
	private static Organizacao organizationA;
	private static Organizacao organizationB;

    private static EntityManager entityManager;
    private static ServicoOrganizacaoImpl organizationService;
    
    /**
	 * Prepara a execu��o de todos os testes.
	 * 
	 * @throws Exception - Se algo sair errado.
	 */    
	@BeforeClass
	public static void setUpBeforeClass() throws Exception
	{
		EntityManagerFactory emf = Persistence.createEntityManagerFactory(
				PropertyTestsUtils.getProperty("persistenceUnit.name"));
	    entityManager = emf.createEntityManager();
	    
	    OrganizacaoDaoImpl organizationDao = new OrganizacaoDaoImpl();
	    organizationDao.setEntityManager(entityManager);
	    
	    organizationService = new ServicoOrganizacaoImpl(organizationDao);
	    
	    final int ORGANIZATIONA_ID = 1;
	    final int ORGANIZATIONB_ID = 2;
	    
	    organizationA = new Organizacao(new Long(ORGANIZATIONA_ID), "IBM",
	    	"International Business Machines");
	    organizationB = new Organizacao(new Long(ORGANIZATIONB_ID), "Accenture", "Escravidão no século 21");
		
	    bdUtils = BDUtils.getInstance();
	    bdUtils.setUpBeforeClass(LOCAL_ARQ + "OrganizationServiceInitialData.xml");
	    bdUtils.setUp();
	}
	
	/**
	 * Prepara a execu��o de cada teste.
	 * 
	 * @throws Exception - Se acontecer algum erro inesperado.
	 */
	@Before
	public void setUp() throws Exception
	{
	    bdUtils.setUp();
	}
    
	/**
	 * Testa a recupera��o da lista de organiza��es.
	 * 
	 * <p>
	 * Teste do m�todo <code>listar()</code>
	 * </p>
	 * 
	 * Cobre o caso GECL02-1
	 */
	@Test
	public void testListar()
	{
		List<Organizacao> expectedOrganizations = new ArrayList<Organizacao>();
		expectedOrganizations.add(organizationA);
		expectedOrganizations.add(organizationB);
		
		assertEquals("A lista de organiza��es retornada � diferente do esperado.",
				expectedOrganizations,
				new ArrayList<Organizacao>(organizationService.listarTodos()));
	}

}
